package com.hd123.rumba.gwt.widget.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.DelegatingChangeListenerCollection;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class LabeledTextBox extends HorizontalPanel implements KeyboardListener, LabeledBox, EnterToTab {
  private Label label;
  private TextBox textBox;
  private String caption;
  private boolean required = false;
  private boolean readOnly = false;

  public LabeledTextBox() {
    setSpacing(4);

    label = new Label();
    label.setWidth("100px");
    label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    add(label);
    setCellVerticalAlignment(label, HorizontalPanel.ALIGN_MIDDLE);

    textBox = createInputBox();
    add(textBox);
    setCellVerticalAlignment(textBox, HorizontalPanel.ALIGN_MIDDLE);

    textBox.addKeyboardListener(this);
  }

  public LabeledTextBox(String caption) {
    this();
    setCaption(caption);
  }

  protected TextBox createInputBox() {
    return new TextBoxE();
  }

  public Label getLabel() {
    return label;
  }

  public TextBox getTextBox() {
    return textBox;
  }

  public void setText(String text) {
    getTextBox().setText(text);
  }

  public String getText() {
    return getTextBox().getText();
  }

  public void setCaption(String caption) {
    this.caption = caption;
    if (required && !readOnly)
      DOM.setInnerHTML(label.getElement(), this.caption + "<font color='red'>*</font>");
    else
      getLabel().setText(caption);
  }

  public String getCaption() {
    return caption;
  }

  public void setRequired(boolean required) {
    this.required = required;
    setCaption(caption);
  }

  public void setReadOnly(boolean readOnly) {
    this.readOnly = readOnly;
    setCaption(this.caption);
    getTextBox().setReadOnly(readOnly);
  }

  public void onKeyDown(Widget sender, char keyCode, int modifiers) {
    keyHandler(sender, keyCode, modifiers);
  }

  public void onKeyPress(Widget sender, char keyCode, int modifiers) {
    keyHandler(sender, keyCode, modifiers);
  }

  public void onKeyUp(Widget sender, char keyCode, int modifiers) {
    keyHandler(sender, keyCode, modifiers);
  }

  private void keyHandler(Widget sender, char keyCode, int modifiers) {
    if (readOnly && !Util.isNavKey(keyCode))
      getTextBox().cancelKey();
  }

  DelegatingChangeListenerCollection changeListeners;

  public void addChangeListener(ChangeListener listener) {
    if (changeListeners == null)
      changeListeners = new DelegatingChangeListenerCollection(this, getTextBox());
    changeListeners.add(listener);
  }

  public void removeChangeListener(ChangeListener listener) {
    if (changeListeners != null)
      changeListeners.remove(listener);
  }

  public void setBoxMaxLength(int length) {
    getTextBox().setMaxLength(length);
  }

  public void setBoxWidth(String width) {
    getTextBox().setWidth(width);
  }

  public void setCaptionWidth(String width) {
    getLabel().setWidth(width);
  }
}
